package com.cheng.project_model1.utils;

import com.github.dozermapper.core.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Chengen
 * @Date: 2018/8/9 15:49
 */
@Service
public class DozerUtil {
    @Autowired
    private Mapper mapper;


    public <T> T map(Object source, Class<T> destinationClass) {
        if (source == null) {
            return null;
        }
        return mapper.map(source, destinationClass);
    }

    public void map(Object source, Object destination) {
        mapper.map(source, destination);
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
        return (List<T>) sourceList.stream().map(it -> mapper.map(it, destinationClass)).collect(Collectors.toList());
    }
}